define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionwebsite/index' + location.search,
                    add_url: 'feditionwebsite/add',
                    edit_url: 'feditionwebsite/edit',
                    del_url: 'feditionwebsite/del',
                    multi_url: 'feditionwebsite/multi',
                    import_url: 'feditionwebsite/import',
                    table: 'fedition_website',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'domain', title: __('Domain'),visible: false,addClass: "selectpage", extend: "data-source='dnslist/index' data-field='name'"},
                        {field: 'domain_name', title: __('Domain'), operate: false},
                        {field: 'account_address', title: __('Account_address'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'account_address_name', title: __('Account_address'), operate: 'LIKE'},
                        {field: 'platform', title: __('Platform'),visible: false,addClass: "selectpage", extend: "data-source='dnsconstruction/index' data-field='system_name'"},
                        {field: 'platform_name', title: __('Platform'), operate: false},
                        {field: 'category', title: __('Category'),visible: false,addClass: "selectpage", extend: "data-source='feditioncategory/index' data-field='name'"},
                        {field: 'category_name', title: __('Category'), operate: false},
                        // {field: 'paymentinfo', title: __('Payment'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'}},
                        {field: 'landing_page', title: __('Landing_page'), searchList: {"0":__('建设中'),"1":__('正常'),"2":__('关闭')}, formatter: Table.api.formatter.status,custom: {0:'warning', 1:'success', 2:'danger'}},
                        {field: 'admin', title: __('Admin'), operate: 'LIKE'},
                        {field: 'password', title: __('Password'), operate: 'LIKE'},
                        // {field: 'order', title: __('Order'), operate: false},
                        // {field: 'amount', title: __('Amount'), operate: false},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'paymenttype', title: __('收款账户类型'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'Paypal': 'info','Stripe':'warning'}},
                        {field: 'admin_id', title: __('Admin_id'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'admin_name', title: __('Admin_id'), operate: false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('收款账户'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                // {
                                //     name: 'detail',
                                //     text: __('新增收款账户'),
                                //     title: __('新增收款账户'),
                                //     classname: 'btn btn-sm btn-info btn-dialog',
                                //     icon: 'fa fa-usd',
                                //     url: 'feditionwebsitepaytype/add/?website_id={ids}'
                                // },
                                {
                                    name: 'detail',
                                    text: __('对应收款账户'),
                                    title: __('对应收款账户'),
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionwebsitepaytype/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        // {
                        //     field: 'buttons',
                        //     width: "120px",
                        //     title: __('广告账户'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     operate: false,
                        //     buttons: [
                        //         {
                        //             name: 'addtabs',
                        //             text: __('对应广告账户'),
                        //             title: __('对应广告账户'),
                        //             classname: 'btn btn-sm btn-warning btn-addtabs',
                        //             icon: 'fa fa-folder-o',
                        //             url: 'feditionaccount/index/?domain={domain}'
                        //         },
                        //     ],
                        //     formatter: Table.api.formatter.buttons
                        // },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作记录'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('操作记录'),
                                    title: __('操作记录'),
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'feditionlog/index/?type=1'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 添加建站平台
            $(document).on("click", ".btn-construction", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("dnsconstruction/add?name=" + name, __('添加建站平台'));
            });
            // 添加店铺
            $(document).on("click", ".btn-accounturl", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("feditionaccounturl/add?name=" + name, __('添加店铺'));
            });
            // 添加域名
            $(document).on("click", ".btn-dnslist", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("dnslist/add?name=" + name, __('添加域名'));
            });
            // 添加产品类别
            $(document).on("click", ".btn-category", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("feditioncategory/add?name=" + name, __('添加产品类别'));
            });
            // 添加收款账户
            $(document).on("click", ".btn-collectiontype", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("feditioncollectiontype/add?name=" + name, __('添加收款账户'));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $("#c-paymentinfo").data("params", function(){
                return {custom: {collection_type: $("#c-PaymentType").val()}};
            });
            Controller.api.bindevent();
            //动态下拉显示多个字段数据信息
            $("#c-paymentinfo").data("formatItem",function (item) {
                return "邮箱账户:"+item.email;
                // return "收款账户:"+item.name+",邮箱账户:"+item.email;
            });
            $("#c-account_address").data("formatItem",function (item) {
                return "店铺名称:"+item.name+",店铺域名:"+item.url;
            });
        },
        edit: function () {
            $("#c-paymentinfo").data("params", function(){
                return {custom: {collection_type: $("#c-PaymentType").val()}};
            });
            Controller.api.bindevent();
            //动态下拉显示多个字段数据信息
            $("#c-paymentinfo").data("formatItem",function (item) {
                return "邮箱账户:"+item.email;
                // return "收款账户:"+item.name+",邮箱账户:"+item.email;
            });
            $("#c-account_address").data("formatItem",function (item) {
                return "店铺名称:"+item.name+",店铺域名:"+item.url;
            });
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        //这里判断是否为分类列表框，也可移除此判断
                        if (self.elem.hidden.is("#c-platform")) {
                            //判断查询记录是否为0，也可移除此判断
                            // if (json.originalResult.length == 0) {
                                $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增建站平台' data-window='top' href='dnsconstruction/add'>新增建站平台</a></p><p style='margin:5px;'>（新增建站平台后需要取消下拉框，重新选择）</p>");
                            // }
                        }
                        if (self.elem.hidden.is("#c-account_address")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增店铺' data-window='top' href='feditionaccounturl/add'>新增店铺</a></p><p style='margin:5px;'>（新增店铺后需要取消下拉框，重新选择）</p>");
                        }
                        if (self.elem.hidden.is("#c-domain")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增对应域名' data-window='top' href='dnslist/add'>新增对应域名</a></p><p style='margin:5px;'>（新增对应域名后需要取消下拉框，重新选择）</p>");
                        }
                        if (self.elem.hidden.is("#c-category")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增产品类别' data-window='top' href='feditioncategory/add'>新增产品类别</a></p><p style='margin:5px;'>（新增产品类别后需要取消下拉框，重新选择）</p>");
                        }
                        if (self.elem.hidden.is("#c-paymentinfo")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增收款账户' data-window='top' href='feditionaccountpay/add'>新增收款账户</a></p><p style='margin:5px;'>（新增收款账户后需要取消下拉框，重新选择）</p>");
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});